"""
Copyright 2012 Wieser Dietmar, Margit Mutschlechner.

This file is part of Multimedia Social Networking Platform.

Multimedia Social Networking Platform is free software: 
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Multimedia Social Networking Platform is distributed
in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Multimedia Social Networking Platform.  
If not, see <http://www.gnu.org/licenses/>.

"""

from PyQt4 import QtGui, QtCore
from msnp.gui import Resources, UIText
from msnp import gui
from msnp.service.network import LeaveRequest

class SystemTray(QtGui.QSystemTrayIcon):
    """This class contains the system tray.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __menu: A QMenu object containing the menu of the system tray.
        __logoutAction: A QAction object containing the logout action connected to the logout() method and added to the menu.
        __quitAction: A QAction object containing the quit action connected to the quit() method and added to the menu.
    """
    
    def __init__(self, parent=None):
        """The init-method."""
        QtGui.QSystemTrayIcon.__init__(self, parent)
        self.__parent = parent
        self.setIcon(QtGui.QIcon(Resources.IMAGE_GROUP_ICON))
        self.__build()
        
    def __build(self):
        """Initializes the logout and quit action, connects them to signals and adds them to the menu. Connects itself to a signal."""
        self.__initActions()
        self.__initMenu()
        self.setContextMenu(self.__menu)
        QtCore.QObject.connect(self, QtCore.SIGNAL('activated(QSystemTrayIcon::ActivationReason)'), self.__activated)
        
    def __initMenu(self):
        """Adds the actions to the menu."""
        self.__menu = QtGui.QMenu(self.__parent)
        self.__menu.addAction(self.__logoutAction)
        self.__menu.addAction(self.__quitAction)
        
    def __initActions(self):
        """Initializes the logout and quit action, connects them to signals."""
        self.__logoutAction = QtGui.QAction(QtGui.QIcon(Resources.IMAGE_LOGOUT), UIText.LABEL_LOGOUT, self)
#        self.__logoutAction.setEnabled(False)
        self.__quitAction = QtGui.QAction(QtGui.QIcon(Resources.IMAGE_EXIT), UIText.LABEL_QUIT, self)
        self.connect(self.__quitAction, QtCore.SIGNAL('triggered()'), self.__quit)
        self.connect(self.__logoutAction, QtCore.SIGNAL('triggered()'), self.__logout)
    
    def enableLogout(self, enable):
        """Enables the logout action."""
        self.__logoutAction.setEnabled(enable)
    
    def __logout(self):
        """This method is connected to the object QAction __logoutAction. Sends a leave request."""
#        gui.MySignal.emitLogoutSignal()
        LeaveRequest.requestFinish(False)
        
    def __quit(self):
        """This method is connected to the object QAction __quitAction. Sends a quit request."""
        LeaveRequest.requestFinish(True)
#        gui.GUIController.getGroupCollectionWidget().destroy()
        
    def __activated(self, reason):
        """This method is connected to the system tray itself. Sets all visible widgets visible/invisible depending on the current visibility."""
        if reason == QtGui.QSystemTrayIcon.Trigger:
            if gui.GUIController.getGroupCollectionWidget() != None:
                if gui.GUIController.getGroupCollectionWidget().isVisible():
                    gui.GUIController.setWidgetsVisible(False)
                else:
                    gui.GUIController.setWidgetsVisible(True)
